<?php 
class CAdvertisementCategoriesController extends CController{

	protected $m_arrobjUsers;
	protected $m_objUser;
	protected $m_objAdvertisementCategorie;
	
	function __construct() {
		parent::__construct();
		return true;
	}
	
	function create() {
		parent::create();
	}
	
	function initialize() {
		parent::initialize();
	}
	
	function execute() {		
		
		switch( $this->m_strAction ) {
			
			case NULL:			
			case 'view_advertisement_categories':
				$this->handleViewAdvertisementCategories();
				break;
				
			case 'add':
			case 'edit':
						
				$this->handleAddEditAdvertisementCategorie();
				break;
							
			case 'insert_update_advertisement_categorie':
					$this->handleInsertUpdateAdvertisementCategorie();
				break;
			case 'update_advertisement_categorie_action':
				$this->handleUpdateAdvertisementCategorieAction();
				break;
			
			case 'view_advertisement_categorie_in_detail':
					$this->handleAdvertisementCategorieInDetail();
			break;
					
			default:
				// Empty default case
				
		}
	}

	/*************************************
	********** HANDLE FUNCTIONS **********
	*************************************/
		
	function handleViewAdvertisementCategories() {
		$this->m_arrobjAdvertisementCategories = CAdvertisementCategories::fetchAdvertisementCategories( $this->m_objDatabase );	
		$this->displayAdvertisementCategories();
	}
	
	function handleAddEditAdvertisementCategorie() {
		$this->m_objAdvertisementCategorie = new CAdvertisementCategorie();
		
		if( true == isset( $_REQUEST['id'] ) ) {
			$this->m_objAdvertisementCategorie = CAdvertisementCategories::fetchAdvertisementCategorieById( $_REQUEST['id'], $this->m_objDatabase );
		}
		
	
		$this->displayAddEditAdvertisementCategorie();
	}
	
	
	function handleInsertUpdateAdvertisementCategorie() {
		
		switch( true ) {
			default:
				$arrmixFieldsValues = $this->m_objData->getPostData( array( 'advertisementcategorie' ) );

				$this->m_objAdvertisementCategorie = CAdvertisementCategories::setValues( 'CAdvertisementCategorie', $arrmixFieldsValues );
				
				/* if( false == $this->m_objAdvertisementCategorie->validate( $arrmixFieldsValues, array_keys( $arrmixFieldsValues ) ) ) {
					break;
				} */
				
				if( false == is_null( $this->m_objAdvertisementCategorie->getId() ) && '' != trim( $this->m_objAdvertisementCategorie->getId() ) ) {
				
					if( false == $this->m_objAdvertisementCategorie->update( $this->m_objDatabase ) ) {
						$this->m_objErrorMessage->setErrorMessage( 'Failed to update AdvertisementCategorie.' );
					} else {
						$this->m_objErrorMessage->setSuccessMessage( 'Advertisement Category update successfully. ' );
					}
				} else {
					
					if( false == $this->m_objAdvertisementCategorie->insert( $_SESSION['udeal.in']['login_user_id'],$this->m_objDatabase ) ) {
						$this->m_objErrorMessage->setErrorMessage( 'Failed to create AdvertisementCategorie.' );
					} else {
						//display( $this->m_objAdvertisementCategorie );die;
						$this->m_objErrorMessage->setSuccessMessage( 'Advertisement Category created successfully. ' );
					}
				}
				redirect( '/admin/advertisement_categorie' );
		}
 		redirect( '/admin/advertisement_categorie/add_edit_advertisement_categorie' );
	}

	function handleUpdateAdvertisementCategorieAction() {
		
	
		switch( true ) {
			default:
	
				$strAction = $this->m_objData->getPostData( array( 'advertisementcategorie', 'action' ) );
				//display($strAction);die;
				if( false == isset( $strAction  ) ) {
					$this->m_objErrorMessage->setErrorMessage( 'Please select action ' );
					break;
				}
	
				$arrintAdvertisementCategorieIds = $this->m_objData->getPostData( array( 'advertisementcategorie', 'id' ) );
				if( false == valArr( $arrintAdvertisementCategorieIds ) ) {
					$this->m_objErrorMessage->setErrorMessage( 'Please select records to delete ' );
					break;
				}
	
				$this->m_objAdvertisementCategorie = new CAdvertisementCategorie();
				if( false == valObj( $this->m_objAdvertisementCategorie, 'CAdvertisementCategorie' ) ) {
					$this->m_objErrorMessage->setErrorMessage( 'Failed to create object' );
					break;
				}
	
				if( 'delete' == $strAction ) {
					if( true == $this->m_objAdvertisementCategorie->deleteAdvertisementCategoriesByIds( $arrintAdvertisementCategorieIds, $this->m_objDatabase ) ) {
						$this->m_objErrorMessage->setSuccessMessage( 'Record(s) deleted successfully.' );
					}
				} else {
					if( true == $this->m_objAdvertisementCategorie->updateAdvertisementCategoriesActionByIds( $strAction, $arrintAdvertisementCategorieIds, $this->m_objDatabase ) ) {
						$this->m_objErrorMessage->setSuccessMessage( 'Record(s) updated successfully.' );
					}
				}
					
		}
		redirect( '/admin/advertisement_categorie' );
	}
	
	
	function handleAdvertisementCategorieInDetail() {
		$intAdvertisementCategorieId = $_REQUEST['id'];
		$this->m_objAdvertisementCategorie = CAdvertisementCategories::fetchAdvertisementCategorieById( $intAdvertisementCategorieId, $this->m_objDatabase );
	
		$this->displayAdvertisementCategorieInDetail();
	}
	
	/**************************************
	********** DISPLAY FUNCTIONS **********
	**************************************/		
	
	function displayAddEditAdvertisementCategorie() {
		$this->display();
	
		//$this->m_objSmarty->assign_by_ref( 'fckeditor', $this->m_objFCKeditor );
		$this->m_objSmarty->assign_by_ref( 'advertisement_categorie', $this->m_objAdvertisementCategorie );
		$this->m_objSmarty->display( TEMPLATE_DIR_PATH . '/admin/advertisement_categorie/view_add_edit_advertisement_categorie.tpl' );
	}
	
	function displayAdvertisementCategories() {
		$this->display();

		$this->m_objSmarty->assign( 'advertisement_categories', $this->m_arrobjAdvertisementCategories );
		$this->m_objSmarty->display( TEMPLATE_DIR_PATH . '/admin/advertisement_categorie/view_advertisement_categories.tpl');

	}
	
	function displayAdvertisementCategorieInDetail() {
		$this->display();
	
		$this->m_objSmarty->assign( 'advertisement_categorie', $this->m_objAdvertisementCategorie );
		$this->m_objSmarty->display( TEMPLATE_DIR_PATH . '/admin/advertisement_categorie/view_advertisement_categorie_in_detail.tpl');
	
	}
	
	function display() {
	
		parent::display();
		$this->m_objSmarty->assign( 'leftadminmenu', 'advertisement_categorie' );
	}

}
?>